package com.ambow.store.controller;

import com.ambow.store.controller.util.Result;
import com.ambow.store.entity.Address;
import com.ambow.store.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("address")
public class AddressController extends BaseController{
    @Autowired
    private AddressService addressService;

    @PostMapping("/adduseraddress")
    public Result addUserAddress(Address address, HttpSession session){
        addressService.addUserAddress(address,session);
        return new Result(OK);
    }

    @GetMapping
    public Result getAddressData(HttpSession session){
        List<Address> addressData = addressService.getAddressData(session);
        return new Result(OK,addressData);
    }

    @GetMapping("setdefault/{aid}")
    public Result setDefaultAddress(@PathVariable("aid") Integer aid, HttpSession session){
        addressService.setDefaultAddress(aid,getUidFromSession(session),getUserNameFromSession(session));
        return new Result(OK);
    }

    @DeleteMapping("delete/{aid}")
    public Result deleteAddress(@PathVariable("aid") Integer aid,HttpSession session){
        addressService.deleteAddress(aid,getUidFromSession(session),getUserNameFromSession(session));
        return new Result(OK);
    }

}



















